package com.scrawler.mysql.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.scrawler.util.MysqlConfig;
import com.scrawler.vo.NewsContent;

public class NewsContentSql {

	
	public static void insert(NewsContent content){
		 Connection conn = MysqlConfig.getConnection();
		 String sql = "insert into news_content(type_id,title,author,content,pub_time,url,hits,comments,reprints)" +
		 		" value(?,?,?,?,?,?,?,?,?) ";
		 PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			
			ps.setInt(1, content.getType_id());
			ps.setString(2, content.getTitle());
			String author = content.getAuthor();
			if(author!=null && author.length()>255){
				author.substring(254);
			}
			ps.setString(3, author);
			ps.setString(4, content.getContent());
			ps.setString(5, content.getPub_time());
			ps.setString(6, content.getUrl());
			String hits = content.getHits();
			if(hits!=null && hits.length()>255){
				hits = hits.substring(254);
			}
			ps.setString(7, hits);
			ps.setString(8, content.getComments());
			ps.setString(9, content.getReprints());
			
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			MysqlConfig.close(conn, ps);
		}
	 }
	
	
	public static void main(String args[]){
		
		NewsContentSql sql = new NewsContentSql();
		NewsContent content = new NewsContent();
		content.setType_id(1);
		content.setAuthor("tk");
		content.setComments("123");
		content.setHits("12");
		content.setPub_time("2013-06o-08 12:12:12");
		content.setReprints("32");
		content.setTitle("nba 测试.");
		content.setUrl("http://www.ifeng.com/");
		content.setContent("");
		
		sql.insert(content);
		
	}
}
